package com.gxan.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxan.common.R;
import com.gxan.pojo.House;
import com.gxan.service.HouseService;
import com.gxan.service.impl.HouseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addHouse")
public class addHouseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("======================service开始前");
        String community_id = req.getParameter("community_id");
        String dong_id = req.getParameter("dong_id");
        String house_num = req.getParameter("house_num");
        String house_name = req.getParameter("house_name");
        String huzhu_name = req.getParameter("huzhu_name");
        String huzhu_tel = req.getParameter("huzhu_tel");
        String house_type = req.getParameter("house_type");
        String house_depart = req.getParameter("house_depart");
        String house_level = req.getParameter("house_level");
        String join_time = req.getParameter("join_time");
        System.out.println(join_time);
        System.out.println("----------------------------------");
        //把数据封装House对象
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-mm-dd");

        House house = new House();
        house.setCommunity_id(Integer.parseInt(community_id));
        house.setDong_id(Integer.parseInt(dong_id));
        house.setHouse_num(house_num);
        house.setHouse_name(house_name);
        house.setHuzhu_name(huzhu_name);
        house.setHuzhu_tel(huzhu_tel);
        house.setHouse_type(house_type);
        house.setHouse_depart(Integer.parseInt(house_depart));
        house.setHouse_level(Integer.parseInt(house_level));
        try {
            house.setJoin_time(sd.parse(join_time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println(house.getJoin_time());
//        null,1,1,'LHNUM001','龙湖金楠天街','左璧莹','15308790123','3室2厅',4,23,'2019-12-28',2
//        house = new House(22,1,1,"LHNUM001","龙湖金楠天街","hh","15308790123","2室2厅",4,23,new Date(),2);
        System.out.println("调用前：" +house);
        //调用service
        HouseService houseService = new HouseServiceImpl();
        R r = houseService.addHouse(house);
        System.out.println("=========service调用后");
        System.out.println("返回后："+house);
        //回写响应
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(r));
    }
}
